package com.ikdong.toodle.dao;

import java.util.ArrayList;
import java.util.List;

import com.ikdong.toodle.model.Account;
import com.ikdong.toodle.util.Constant;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.util.Log;

public class AccountDAO {
	private DataBaseHelper db;
	
	public AccountDAO(Context context){
		db = new DataBaseHelper(context);
	}
	
	public Account get(String userid){
		Account account = null;
		Cursor cursor = null;
		try{
			db.openDataBase();
			String where = "userid='"+userid+"'";
			cursor = db.getReadableDatabase().query(true, "Account", Account.getColumns(), where,null, null, null, null, null);
			if(cursor!=null && cursor.moveToFirst()){
				account = initAccount(cursor);
			}
		}catch(Exception e){
			Log.e(Constant.TAG, "AccountDAO.get(userid) error", e);
		}finally{
			if(cursor!=null) cursor.close();
			if(db!=null) db.close();
		}
		
		return account;
	}
	
	public List<Account> get(){
		List<Account> accounts = new ArrayList<Account>();
		Cursor cursor = null;
		try{
			db.openDataBase();
			cursor = db.getReadableDatabase().query(true, "Account", Account.getColumns(), null,null, null, null, null, null);
			if(cursor!=null && cursor.moveToFirst()){
				do{
					accounts.add(initAccount(cursor));
				}while(cursor.moveToNext());
				
			}
		}catch(Exception e){
			Log.e(Constant.TAG, "AccountDAO.get() error", e);
		}finally{
			if(cursor!=null) cursor.close();
			if(db!=null) db.close();
		}
		
		return accounts;
	}
	
	public boolean add(Account account){
		long rlt = -1;
		try{
			db.openDataBase();
			ContentValues values = new ContentValues();
			values.put("userid", account.getUserid());
			values.put("alias", account.getAlias());
			values.put("pro", account.getPro());
			values.put("dateformat", account.getDateformat());
			values.put("timezone", account.getTimezone());
			values.put("hidemonths", account.getHidemonths());
			values.put("hotlistpriority", account.getHotlistpriority());
			values.put("hotlistduedate", account.getHotlistduedate());
			values.put("hotliststar", account.getHotliststar());
			values.put("hotliststatus", account.getHotliststatus());
			values.put("showtabnums", account.getShowtabnums());
			values.put("lastedit_task", account.getLastedit_task());
			values.put("lastdelete_task", account.getLastdelete_task());
			values.put("lastedit_folder", account.getLastedit_folder());
			values.put("lastedit_context", account.getLastedit_context());
			values.put("lastedit_goal", account.getLastedit_goal());
			values.put("lastedit_location", account.getLastedit_location());
			values.put("lastedit_notebook", account.getLastedit_notebook());
			values.put("lastdelete_notebook", account.getLastdelete_notebook());
			rlt = db.getWritableDatabase().insert("Account", null, values);	
		}catch(Exception e){
			Log.e(Constant.TAG, "AccountDAO.add() error", e);
		}finally{
			if(db!=null) db.close();
		}
		
		return rlt>-1;
	}
	
	public boolean update(Account account){
		long rlt = 0;
		try{
			db.openDataBase();
			String where = "userid="+account.getUserid();
			ContentValues values = new ContentValues();
			if(account.getUserid()!=null)
				values.put("userid", account.getUserid());
			if(account.getAlias()!=null)
				values.put("alias", account.getAlias());
			if(account.getPro()!=null)
				values.put("pro", account.getPro());
			if(account.getDateformat()!=null)
				values.put("dateformat", account.getDateformat());
			if(account.getTimezone()!=null)
				values.put("timezone", account.getTimezone());
			if(account.getHidemonths()!=null)
				values.put("hidemonths", account.getHidemonths());
			if(account.getHotlistpriority()!=null)
				values.put("hotlistpriority", account.getHotlistpriority());
			if(account.getHotlistduedate()!=null)
				values.put("hotlistduedate", account.getHotlistduedate());
			if(account.getHotliststar()!=null)
				values.put("hotliststar", account.getHotliststar());
			if(account.getHotliststatus()!=null)
				values.put("hotliststatus", account.getHotliststatus());
			if(account.getShowtabnums()!=null)
				values.put("showtabnums", account.getShowtabnums());
			if(account.getLastedit_task()!=null)
				values.put("lastedit_task", account.getLastedit_task());
			if(account.getLastdelete_task()!=null)
				values.put("lastdelete_task", account.getLastdelete_task());
			if(account.getLastedit_folder()!=null)
				values.put("lastedit_folder", account.getLastedit_folder());
			if(account.getLastedit_context()!=null)
				values.put("lastedit_context", account.getLastedit_context());
			if(account.getLastedit_goal()!=null)
				values.put("lastedit_goal", account.getLastedit_goal());
			if(account.getLastedit_location()!=null)
				values.put("lastedit_location", account.getLastedit_location());
			if(account.getLastedit_notebook()!=null)
				values.put("lastedit_notebook", account.getLastedit_notebook());
			if(account.getLastdelete_notebook()!=null)
				values.put("lastdelete_notebook", account.getLastdelete_notebook());
			rlt = db.getWritableDatabase().update("Account", values, where, null);
		}catch(Exception e){
			Log.e(Constant.TAG, "AccountDAO.update() error", e);
		}finally{
			if(db!=null) db.close();
		}
		
		return rlt>0;
	}
	
	public boolean delete(){
		long rlt = 0;
		try{
			db.openDataBase();
			rlt = db.getWritableDatabase().delete("Account", null,null);
		}catch(Exception e){
			Log.e(Constant.TAG, "AccountDAO.delete() error", e);
		}finally{
			if(db!=null) db.close();
		}
		
		return rlt>0;
	}
	
	private Account initAccount(Cursor cursor){
		Account account = null;
		if(cursor!=null){
			account = new Account();
			account.setUserid(cursor.getString(0));
			account.setAlias(cursor.getString(1));
			account.setPro(cursor.getLong(2));
			account.setDateformat(cursor.getLong(3));
			account.setTimezone(cursor.getLong(4));
			account.setHidemonths(cursor.getLong(5));
			account.setHotlistpriority(cursor.getLong(6));
			account.setHotlistduedate(cursor.getLong(7));
			account.setHotliststar(cursor.getLong(8));
			account.setHotliststatus(cursor.getLong(9));
			account.setShowtabnums(cursor.getLong(10));
			account.setLastedit_task(cursor.getLong(11));
			account.setLastdelete_task(cursor.getLong(12));
			account.setLastedit_folder(cursor.getLong(13));
			account.setLastedit_context(cursor.getLong(14));
			account.setLastedit_goal(cursor.getLong(15));
			account.setLastedit_location(cursor.getLong(16));
			account.setLastedit_notebook(cursor.getLong(17));
			account.setLastdelete_notebook(cursor.getLong(18));
		}
		
		return account;
	}
}
